#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Neatly.Module;
using UnityEngine;

namespace Neatly.Load.Manager
{
    public class AssetBundleMapManager : Singleton<AssetBundleMapManager>
    {
        private readonly Dictionary<string, string> m_AssetBundleMap = new();
        private LoadModule m_LoadModule;

        public async UniTask Init(LoadModule loadModule)
        {
            m_LoadModule = loadModule;
            if (!NeatlyConfig.AssetBundleEnable) return;
            m_AssetBundleMap.Clear();
            var loader = await m_LoadModule.LoadAssetBundleDirect(NeatlyConfig.NAME_ASSET_BUNDLE_MAP);
            var bundle = loader.ResultObject as AssetBundle;
            var bundleMap = bundle.LoadAsset<AssetBundleMap>(NeatlyConfig.NAME_ASSET_BUNDLE_MAP);
            for (int i = 0; i < bundleMap.path.Length; i++)
            {
                m_AssetBundleMap.Add(bundleMap.path[i], bundleMap.bundleName[i]);
            }
        }

        public string Get(string key)
        {
            key = key.ToLower();
            return m_AssetBundleMap.GetValueOrDefault(key, key);
        }
    }
}